package main;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author chau
 */
public class Recorder {

    private RecordStore rs = null;
    static final String REC_STORE = "db";
//    private int recordIndex = 1;
    //   private int numberRecord;
    private String userName = "";
    private String passWord = "";

    public void openRecStore() {
        try {
            // The second parameter indicates that the record store
            // should be created if it does not exist
            rs = RecordStore.openRecordStore(REC_STORE, true);
            // numberRecord = rs.getNumRecords();
            read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void closeRecStore() {
        try {
            rs.closeRecordStore();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteRecStore() {
        try {
            RecordEnumeration renum = rs.enumerateRecords(null, null, false);
            while (renum.hasNextElement()) {
                int index = renum.nextRecordId();
                rs.deleteRecord(index);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void writeRecord(String str) {
        Debug.d("write :" + str);
        byte[] rec = str.getBytes();
        try {
            rs.addRecord(rec, 0, rec.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public String readRecords() {
//        try {
//            Debug.d("num record :" + numberRecord);
//            // Intentionally make this too small to test code below
//            Debug.d("index :" + recordIndex);
//            Debug.d("size :" + rs.getRecordSize(recordIndex));
//            byte[] recData = new byte[rs.getRecordSize(recordIndex)];
//            int len = rs.getRecord(recordIndex, recData, 0);
//            String stringRead = new String(recData, 0, len);
//            Debug.d("read :" + stringRead);
//            recordIndex++;
//            return stringRead;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }
    private void read() {
        String[] result = new String[2];
        try {
            RecordEnumeration renum = rs.enumerateRecords(null, null, false);
            while (renum.hasNextElement()) {
                int index = renum.nextRecordId();
                Debug.d("index :" + index);
//                Debug.d("size :" + rs.getRecordSize(index));
                byte[] recData = new byte[rs.getRecordSize(index)];
                int len = rs.getRecord(index, recData, 0);
                result[(index - 1) % 2] = new String(recData, 0, len);
                Debug.d("read :" + result[(index - 1) % 2]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Debug.d("string 1 :" + result[0]);
        Debug.d("string 2 :" + result[1]);
        userName = result[0];
        passWord = result[1];
    }

    public String getUserName() {
        return userName;
    }

    public String getPassWord() {
        return passWord;
    }
//    public void readRecords() {
//        try {
//            // Intentionally make this too small to test code below
//            byte[] recData = new byte[50];
//            int len;
//
//            for (int i = 1; i <= rs.getNumRecords(); i++) {
//                if (rs.getRecordSize(i) > recData.length) {
//                    recData = new byte[rs.getRecordSize(i)];
//                }
//
//                len = rs.getRecord(i, recData, 0);
//                
//                System.out.println("Record #" + i + ": " + new String(recData, 0, len));
//                System.out.println("------------------------------");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
